/**
 * Copyright 2016-2017 TVI Go Easy.
 * Created on 2017/4/14 01:38
 */
package org.mechanic.fund.domain.system.manager;

import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import org.apache.commons.lang3.builder.ToStringBuilder;
import org.mechanic.fund.domain.BaseEntity;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.JoinColumn;
import javax.persistence.JoinTable;
import javax.persistence.ManyToMany;
import javax.persistence.ManyToOne;
import javax.persistence.Table;
import javax.persistence.Transient;
import java.util.HashSet;
import java.util.Set;

/**
 * 用户信息实体
 *
 * @author mechanic
 * @version 0.0.1
 */
@Entity
@Table(name = "sys_user")
public class User extends BaseEntity {

    private static final long serialVersionUID = -8563756919814096577L;
    /**
     * 登录账号
     */
    @Column(name = "account")
    private String account;
    /**
     * 登录密码
     */
    @Column(name = "password")
    @JsonIgnore
    private String password;
    /**
     * 加密盐值
     */
    @Column(name = "salt")
    @JsonIgnore
    private String salt;
    /**
     * 成员所属部门
     */
    @ManyToOne//(cascade = {CascadeType.REMOVE, CascadeType.MERGE})
    @JoinColumn(name = "department_id")
    @JsonIgnoreProperties(value = {"sort", "children", "name", "wxParent"})
    private Department department;
    /**
     * 真实姓名
     */
    @Column(name = "name")
    private String name;
    /**
     * 真实姓名拼音
     */
    @Column(name = "name_spell")
    private String nameSpell;
    /**
     * 二维码
     */
    @Column(name = "qrcode")
    private String qrcode;
    /**
     * 身份证号
     */
    @Column(name = "identity_card")
    private String identityCard;
    /**
     * 通讯地址
     */
    @Column(name = "address")
    private String address;
    /**
     * 是否首次登录。0=否，1=是
     */
    @Column(name = "is_first_login")
    private Integer isFirstLogin;

    //=================================================
    // 微信企业号相关
    //=================================================
    /**
     * 成员UserID。对应管理端的帐号。
     * 对应微信字段：userid
     */
    @Column(name = "wx_user_id")
    private String wxUserId;
    /**
     * 成员名称
     * 对应微信字段：name
     */
    @Column(name = "wx_name")
    private String wxName;
    /**
     * 成员所属部门id列表
     * 对应微信字段：department
     */
    //@Column(name = "wx_department")
    //private String wx_department;
    /**
     * 职位信息
     * 对应微信字段：position
     */
    @Column(name = "wx_position")
    private String wxPosition;
    /**
     * 手机号码。第三方不可获取
     * 对应微信字段：mobile
     */
    @Column(name = "wx_mobile")
    private String wxMobile;
    /**
     * 微信 openid。用户在某个公众号或小程序内部的唯一编号
     * 对应微信字段：openid
     */
    @Column(name = "wx_open_id")
    private String wxOpenId;
    /**
     * 微信 unionid。用户在其已关注的所有公众号或小程序内部通用的唯一编号
     * 对应微信字段：unionid
     */
    @Column(name = "wx_union_id")
    private String wxUnionId;
    /**
     * 性别。0表示未定义，1表示男性，2表示女性
     * 对应微信字段：gender
     */
    @Column(name = "wx_gender")
    private Integer wxGender;
    /**
     * 邮箱。第三方不可获取
     * 对应微信字段：email
     */
    @Column(name = "wx_email")
    private String wxEmail;
    /**
     * 微信号
     * 对应微信字段：weixinid
     */
    @Column(name = "wei_xin_id")
    private String weiXinId;
    /**
     * 头像url。注：如果要获取小图将url最后的"/0"改成"/64"即可
     * 对应微信字段：avatar
     */
    @Column(name = "wx_avatar")
    private String wxAvatar;
    /**
     * 关注状态: 1=已关注，2=已禁用，4=未关注
     * 对应微信字段：status
     */
    @Column(name = "wx_status")
    private Integer wxStatus;
    /**
     * 启用/禁用成员。1表示启用成员，0表示禁用成员
     * 对应微信字段：enable
     */
    @Column(name = "wx_enable")
    private Integer wxEnable;
    /**
     * 扩展属性。第三方不可获取
     * 对应微信字段：extattr
     */
    @Column(name = "wx_extattr")
    private String wxExtattr;
    /**
     * 角色
     */
    @ManyToMany(fetch = FetchType.EAGER)
    @JoinTable(name = "sys_user_role",
      joinColumns = {@JoinColumn(name = "user_id")},
      inverseJoinColumns = {@JoinColumn(name = "role_id")})
    @JsonIgnoreProperties(value = {"description", "users", "sort"})
    private Set<Role> roles;
    /**
     * 标签
     */
    @ManyToMany(fetch = FetchType.EAGER)
    @JoinTable(name = "sys_user_tag",
      joinColumns = {@JoinColumn(name = "user_id")},
      inverseJoinColumns = {@JoinColumn(name = "tag_id")})
    @JsonIgnoreProperties(value = {"description", "users", "sort"})
    private Set<Tag> tags;

    public User() {
    }

    public User(
      //String id,
      String account,
      String password,
      String salt,
      Department department,
      String name,
      String nameSpell,
      String qrcode,
      String identityCard,
      String address,
      Integer isFirstLogin,
      String wxUserId,
      String wxName,
      //String wx_department,
      String wxPosition,
      String wxMobile,
      String wxOpenId,
      String wxUnionId,
      Integer wxGender,
      String wxEmail,
      String weiXinId,
      String wxAvatar,
      Integer wxStatus,
      Integer wxEnable,
      String wxExtattr) {
        super();
        this.account = account;
        this.password = password;
        this.salt = salt;
        this.department = department;
        this.name = name;
        this.nameSpell = nameSpell;
        this.qrcode = qrcode;
        this.identityCard = identityCard;
        this.address = address;
        this.isFirstLogin = isFirstLogin;
        this.wxUserId = wxUserId;
        this.wxName = wxName;
        this.wxPosition = wxPosition;
        this.wxMobile = wxMobile;
        this.wxOpenId = wxOpenId;
        this.wxUnionId = wxUnionId;
        this.wxGender = wxGender;
        this.wxEmail = wxEmail;
        this.weiXinId = weiXinId;
        this.wxAvatar = wxAvatar;
        this.wxStatus = wxStatus;
        this.wxEnable = wxEnable;
        this.wxExtattr = wxExtattr;
    }

    public String getAccount() {
        return account;
    }

    public void setAccount(String account) {
        this.account = account;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    public String getSalt() {
        return salt;
    }

    public void setSalt(String salt) {
        this.salt = salt;
    }

    public Department getDepartment() {
        return department;
    }

    public void setDepartment(Department department) {
        this.department = department;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getNameSpell() {
        return nameSpell;
    }

    public void setNameSpell(String nameSpell) {
        this.nameSpell = nameSpell;
    }

    public String getQrcode() {
        return qrcode;
    }

    public void setQrcode(String qrcode) {
        this.qrcode = qrcode;
    }

    public String getIdentityCard() {
        return identityCard;
    }

    public void setIdentityCard(String identityCard) {
        this.identityCard = identityCard;
    }

    public String getAddress() {
        return address;
    }

    public void setAddress(String address) {
        this.address = address;
    }

    public Integer getIsFirstLogin() {
        return isFirstLogin;
    }

    public void setIsFirstLogin(Integer isFirstLogin) {
        this.isFirstLogin = isFirstLogin;
    }

    public String getWxUserId() {
        return wxUserId;
    }

    public void setWxUserId(String wxUserId) {
        this.wxUserId = wxUserId;
    }

    public String getWxName() {
        return wxName;
    }

    public void setWxName(String wxName) {
        this.wxName = wxName;
    }

    //public String getWx_department() {
    //    return wx_department;
    //}
    //
    //public void setWx_department(String wx_department) {
    //    this.wx_department = wx_department;
    //}

    public String getWxPosition() {
        return wxPosition;
    }

    public void setWxPosition(String wxPosition) {
        this.wxPosition = wxPosition;
    }

    public String getWxMobile() {
        return wxMobile;
    }

    public void setWxMobile(String wxMobile) {
        this.wxMobile = wxMobile;
    }

    public String getWxOpenId() {
        return wxOpenId;
    }

    public void setWxOpenId(String wxOpenId) {
        this.wxOpenId = wxOpenId;
    }

    public String getWxUnionId() {
        return wxUnionId;
    }

    public void setWxUnionId(String wxUnionId) {
        this.wxUnionId = wxUnionId;
    }

    public Integer getWxGender() {
        return wxGender;
    }

    public void setWxGender(Integer wxGender) {
        this.wxGender = wxGender;
    }

    public String getWxEmail() {
        return wxEmail;
    }

    public void setWxEmail(String wxEmail) {
        this.wxEmail = wxEmail;
    }

    public String getWeiXinId() {
        return weiXinId;
    }

    public void setWeiXinId(String weiXinId) {
        this.weiXinId = weiXinId;
    }

    public String getWxAvatar() {
        return wxAvatar;
    }

    public void setWxAvatar(String wxAvatar) {
        this.wxAvatar = wxAvatar;
    }

    public Integer getWxStatus() {
        return wxStatus;
    }

    public void setWxStatus(Integer wxStatus) {
        this.wxStatus = wxStatus;
    }

    public Integer getWxEnable() {
        return wxEnable;
    }

    public void setWxEnable(Integer wxEnable) {
        this.wxEnable = wxEnable;
    }

    public String getWxExtattr() {
        return wxExtattr;
    }

    public void setWxExtattr(String wxExtattr) {
        this.wxExtattr = wxExtattr;
    }

    public Set<Role> getRoles() {
        return roles;
    }

    public void setRoles(Set<Role> roles) {
        this.roles = roles;
    }

    public Set<Tag> getTags() {
        return tags;
    }

    public void setTags(Set<Tag> tags) {
        this.tags = tags;
    }

    /**
     * 返回角色名称集合
     *
     * @return 权限名称集合
     */
    @Transient //非数据库字段
    @JsonIgnore
    public Set<String> getRolesName() {
        Set<Role> roles = getRoles();
        Set<String> set = new HashSet<String>();
        for (Role role : roles) {
            set.add(role.getName());
        }
        return set;
    }

    @Override
    public String toString() {
        return ToStringBuilder.reflectionToString(this);
    }
}
